﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace JCFramework.Extensions
{
    public static class DataTimeExtensions
    {
        public static int ConvertToInt32(this DateTime date)
        {
            var str = date.ToString("yyyyMMdd");
            return str.ToInt32();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static DateTime ConvertToDateTime(this Int32 value)
        {
            var str = value.ToString();
            var result = DateTime.Now;
            if (DateTime.TryParseExact(str, "yyyyMMdd", null, System.Globalization.DateTimeStyles.None, out result))
            {
                return result;
            }

            throw new FormatException("value convert to DateTime is error;");
        }

        public static DateTime TickToDateTime(this long tick)
        {
            return new DateTime(tick);
        }

        public static string TickToDateTimeToString(this long tick, string format)
        {
            return TickToDateTime(tick).ToString(format);
        }

        public static long ToTimesamp(this DateTime date)
        {
            return Decimal.ToInt64(Decimal.Divide(date.Ticks - new DateTime(1970, 1, 1, 8, 0, 0).Ticks, 10000));
        }
    }
}
